<template>
  <div class="container">
    <div class="app-container">
      <el-card>
        <div class="edit-form">
          <el-form ref="userForm" size="medium" :rules="rules" :model="userInfo" label-width="220px">
            <!-- 姓名 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="姓名" prop="username">
                  <el-input v-model="userInfo.username" size="mini" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 工号 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="工号" prop="workNumber">
                  <el-input v-model="userInfo.workNumber" size="mini" class="inputW" disabled />
                </el-form-item>
              </el-col>
            </el-row>
            <!--手机  -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="手机" prop="mobile">
                  <el-input v-model="userInfo.mobile" :disabled="!!$route.params.id" size="mini" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="部门" prop="departmentId">
                  <!-- 要想 prop 支持校验的能力，这儿应该写 v-model="xxx.departmentId" -->
                  <!-- v-bind:value 和 @input 的语法糖 -->
                  <!-- <select-tree :value="userInfo.departmentId" @input="userInfo.departmentId = $event" /> -->
                  <!-- 和上面等价 -->
                  <select-tree v-model="userInfo.departmentId" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="聘用形式" prop="formOfEmployment">
                  <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                    <el-option label="正式" :value="1" />
                    <el-option label="非正式" :value="2" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="入职时间" prop="timeOfEntry">
                  <el-date-picker v-model="userInfo.timeOfEntry" size="mini" type="date" value-format="yyyy-MM-dd" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="转正时间" prop="correctionTime">
                  <el-date-picker v-model="userInfo.correctionTime" size="mini" type="date" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 员工照片 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="员工头像">
                  <ImageUpload v-model="userInfo.staffPhoto" />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 保存个人信息 -->
            <el-row type="flex">
              <el-col :span="12" style="margin-left:220px">
                <el-button size="mini" type="primary" @click="editOrAddEmploy">保存更新</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
import SelectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'
export default {
  name: 'EmployeeDetail',
  components: {
    SelectTree,
    ImageUpload
  },
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门 id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: ''
      },
      rules: {
        username: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }, {
          min: 1,
          max: 4,
          message: '姓名为1-4位'
        }],
        mobile: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{
          required: true,
          message: '请选择聘用形式',
          trigger: 'blur'
        }],
        departmentId: [{
          required: true,
          message: '请选择部门',
          trigger: 'change'
        }],
        timeOfEntry: [{
          required: true,
          message: '请选择入职时间',
          trigger: 'blur'
        }],
        correctionTime: [{
          required: true,
          message: '请选择转正时间',
          trigger: 'blur'
        }, {
          validator: (_, value, callback) => {
            // 日期对象之间可以直接比较，内部会转成时间戳进行处理
            if (this.userInfo.timeOfEntry) {
              // 转正时间要大于入职时间
              if (new Date(value) < new Date(this.userInfo.timeOfEntry)) {
                return callback(new Error('转正时间要大于入职时间'))
              }
            }
            // 校验通过
            callback()
          }
        }]
      }
    }
  },
  computed: {
    empId() {
      return this.$route.params.id
    }
  },
  created() {
    // 编辑的时候才去获取详情
    if (this.empId) this.getEmployeeDetail()
  },
  methods: {
    editOrAddEmploy() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          try {
            if (this.empId) {
              // 编辑
              await updateEmployee(this.userInfo)
            } else {
              await addEmployee(this.userInfo)
            }
            this.$message.success((this.empId ? '编辑' : '新增') + '成功')
            // 跳转到列表页
            this.$router.push('/employee')
          } catch (e) {
            console.log(e)
          }
        }
      })
    },
    async getEmployeeDetail() {
      this.userInfo = await getEmployeeDetail(this.empId)
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
        background: #fff;
        padding: 20px;

        .inputW {
            width: 380px
        }
    }
</style>
